package util;

import dao.LogDao;
import dao.impl.LogDaoImpl;
import pojo.Log;
import pojo.Worker;

import javax.servlet.http.HttpServletRequest;

public class LogUtil {

    private static LogDao logDao = new LogDaoImpl();

    /**
     *  添加日志信息(必是成功)
     * @param content
     * @param request
     */
    public static void addLog(String content, HttpServletRequest request){
        Log log = new Log();
        log.setLogContent(content);
        Worker worker = (Worker) request.getSession().getAttribute("worker");
        log.setLogWorkerName(worker.getWorkerName());
        logDao.addLog(log);
    }
    /**
     *  添加自定义日志信息（更具code判断是否成功）
     * @param content
     * @param code
     */
    public static void addLog(String content,HttpServletRequest request, Integer code){
        Worker worker = (Worker) request.getSession().getAttribute("worker");
        if (worker==null){
            logDao.addLog(content,code);
        }
        else {
            Log log = new Log();
            log.setLogContent(content);
            log.setLogWorkerName(worker.getWorkerName());
            log.setLogIsSuccess(code);
            logDao.addLog(log,code);
        }
    }

}
